﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Data;

namespace WebshopPizzaRio
{
    public partial class Pizzadetails : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            PizzaBLL pbll = new PizzaBLL();

            MembershipUser currentuserid = Membership.GetUser();
            int number;
            if (int.TryParse(Request.QueryString["id"], out number))
                {
            try
            {
                if ((Guid)currentuserid.ProviderUserKey == null)
                {
                    throw new NullReferenceException();
                }
                else
                {
                    Guid userid = (Guid)currentuserid.ProviderUserKey;
                    ListView1.DataSourceID = "SqlDataSource3";
                    ListView1.DataBind();
                }
            }
            catch (NullReferenceException)
            {
                ListView1.DataSourceID = "SqlDataSource1";

                    foreach (WebshopPizzaRio.App_Code.DAL.PizzaDAL.pizzaRow row in pbll.getPizzaIDS())
                    {
                        if (Request.QueryString["id"] != "" && Request.QueryString["id"] != null && Request.QueryString["id"].ToString() == row[0].ToString())
                        {
                            ErrorincorrectQueryString.Visible = false;
                            Label1.Visible = true;
                            ListView1.Visible = true;
                            ListView2.Visible = true;
                            break;
                        }
                        else
                        {
                            ErrorincorrectQueryString.Visible = true;
                            Label1.Visible = false;
                            ListView1.Visible = false;
                            ListView2.Visible = false;
                        }
                    }
                    ListView1.DataBind();
                }}
                else
                {
                    ErrorincorrectQueryString.Visible = true;
                    Label1.Visible = false;
                    ListView1.Visible = false;
                    ListView2.Visible = false;
                }
            }

        protected void ListView1_SelectedIndexChanging(object sender, ListViewSelectEventArgs e)
        {
                ListView1.SelectedIndex = e.NewSelectedIndex;

                ShoppingCart cart = (ShoppingCart)Session["shoppingcart"];

                Items newpizza = cart.getNewItem();
                newpizza.PizzaNaam = ListView1.SelectedDataKey.Values["Pizza"].ToString();
                decimal pps = decimal.Parse(ListView1.SelectedDataKey.Values["Prijs"].ToString());
                newpizza.PrijsperStuk = pps;
                newpizza.Aantal = 1;
                newpizza.PizzaID = int.Parse(ListView1.SelectedDataKey.Values["pizzaid"].ToString());
                cart.AddItem(newpizza);

                ListView listviewmp;
                listviewmp = (ListView)(Master).FindControl("ListView1");
                listviewmp.DataBind();

                Label label1mp;
                label1mp = (Label)(Master).FindControl("label1");
                label1mp.Text = "Totaal bedrag: € " + cart.getTotaalBedrag();
        }

        protected void SqlDataSource3_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            if (e.AffectedRows < 1)
            {
                ErrorincorrectQueryString.Visible = true;
                Label1.Visible = false;
                ListView1.Visible = false;
                ListView2.Visible = false;
            }
        }
    }
}